package com.whosly.strategy.manager;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.whosly.strategy.IStrategy;
import com.whosly.strategy.container.IStrategyContainer;

import java.util.Set;

/**
 * 使用饿汉模式实现，
 *
 * 懒汉模式的实现可以提前被加载和分配。
 *
 * IStrategyContainer 管理
 *
 * @Author yueny09 <deep_blue_yang@126.com>
 * @Date 2019-09-05 15:26
 */
public class ContainerFactoryHolder<C, S extends IStrategy<C>> {
    // volatile 避免重排序
    private static volatile ContainerFactoryHolder holder;
    public static ContainerFactoryHolder get(){
        if(holder == null){
            synchronized (ContainerFactoryHolder.class){
                if(holder == null){
                    holder = new ContainerFactoryHolder();
                }
            }
        }

        return holder;
    }

    // R, C, V
    private final Table<String, IStrategyContainer<C, S>, Class<IStrategyContainer<C, S>>> factory = HashBasedTable.create();;

    public void registry(String key, IStrategyContainer<C, S> strategyContainerInterface, Class<IStrategyContainer<C, S>> clazz) {
        // R rowKey, C columnKey, V value
        factory.put(key, strategyContainerInterface, clazz);
    }

    public Set<String> getRows() {
        return factory.rowKeySet();
    }

}
